package com.flnet.web.controller.foxwash;

import com.flnet.common.annotation.Log;
import com.flnet.common.base.AjaxResult;
import com.flnet.common.enums.BusinessType;
import com.flnet.common.page.TableDataInfo;
import com.flnet.common.utils.UUIDUtils;
import com.flnet.common.utils.poi.ExcelUtil;
import com.flnet.foxwash.domain.WServiceItem;
import com.flnet.foxwash.domain.WServiceType;
import com.flnet.foxwash.domain.WShop;
import com.flnet.foxwash.service.IWServiceItemService;
import com.flnet.foxwash.service.IWServiceTypeService;
import com.flnet.framework.util.ShiroUtils;
import com.flnet.framework.web.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务/商品 信息操作处理
 * 
 * @author flnet
 * @date 2019-03-20
 */
@Controller
@RequestMapping("/foxwash/wServiceItem")
public class WServiceItemController extends BaseController
{
    private String prefix = "foxwash/wServiceItem";
	
	@Autowired
	private IWServiceItemService wServiceItemService;

	@Autowired
	private IWServiceTypeService wServiceTypeService;
	
	@RequiresPermissions("foxwash:wServiceItem:view")
	@GetMapping()
	public String wServiceItem()
	{
	    return prefix + "/wServiceItem";
	}
	
	/**
	 * 查询服务/商品列表
	 */
	@RequiresPermissions("foxwash:wServiceItem:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WServiceItem wServiceItem)
	{
		startPage();
        List<WServiceItem> list = wServiceItemService.selectWServiceItemList(wServiceItem);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出服务/商品列表
	 */
	@RequiresPermissions("foxwash:wServiceItem:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WServiceItem wServiceItem)
    {
    	List<WServiceItem> list = wServiceItemService.selectWServiceItemList(wServiceItem);
        ExcelUtil<WServiceItem> util = new ExcelUtil<WServiceItem>(WServiceItem.class);
        return util.exportExcel(list, "wServiceItem");
    }
	
	/**
	 * 新增服务/商品
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		mmap.addAttribute("serviceTypeList",wServiceTypeService.selectWServiceTypeList(null));
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存服务/商品
	 */
	@RequiresPermissions("foxwash:wServiceItem:add")
	@Log(title = "服务/商品", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WServiceItem wServiceItem)
	{
		wServiceItem.setUuid(UUIDUtils.getUUID());
		wServiceItem.setCreator(ShiroUtils.getUserId());
		return toAjax(wServiceItemService.insertWServiceItem(wServiceItem));
	}

	/**
	 * 修改服务/商品
	 */
	@GetMapping("/edit/{itemId}")
	public String edit(@PathVariable("itemId") Long itemId, ModelMap mmap)
	{
		WServiceItem wServiceItem = wServiceItemService.selectWServiceItemById(itemId);
		mmap.put("wServiceItem", wServiceItem);

		WServiceType serviceDO = wServiceTypeService.selectWServiceTypeById(wServiceItem.getTypeId());
		List<WServiceType> serviceList = wServiceTypeService.selectWServiceTypeList(null);
		WServiceType s = new WServiceType();
		if(serviceDO!=null && serviceDO.getTypeId()!=null && serviceList!=null){
			for(WServiceType service:serviceList){
				if(service.getTypeId()==serviceDO.getTypeId()){
					s = service;
				}
			}
			serviceList.remove(s);
		}
		mmap.addAttribute("typeList",serviceList);
		mmap.addAttribute("type",serviceDO);

	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存服务/商品
	 */
	@RequiresPermissions("foxwash:wServiceItem:edit")
	@Log(title = "服务/商品", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WServiceItem wServiceItem)
	{
		wServiceItem.setModifier(ShiroUtils.getUserId());
		return toAjax(wServiceItemService.updateWServiceItem(wServiceItem));
	}
	
	/**
	 * 删除服务/商品
	 */
	@RequiresPermissions("foxwash:wServiceItem:remove")
	@Log(title = "服务/商品", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(wServiceItemService.deleteWServiceItemByIds(ids));
	}

	/**
	 * 服务状态修改
	 */
	@Log(title = "服务状态管理", businessType = BusinessType.UPDATE)
	@RequiresPermissions("foxwash:wserviceItem:edit")
	@PostMapping("/changeStatus")
	@ResponseBody
	public AjaxResult changeStatus(WServiceItem serviceItem)
	{
		return toAjax(wServiceItemService.changeStatus(serviceItem));
	}
}
